package cn.broadmax.boot;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import jexx.util.StringUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jeff
 * @date 2020/6/14
 */
public class CodeGenerator {

    @Test
    @Disabled
    public void generateCode(){
        String projectPath = "D:\\test\\work\\broadmax\\qq1\\aj";
        String url = "jdbc:mysql://192.168.1.198:3306/broadmax?useUnicode=true&useSSL=false&characterEncoding=utf8";
        String userName = "root";
        String password = "123456";
        String driver = "com.mysql.cj.jdbc.Driver";
        String packetName = "cn.broadmax.boot.modules";

        String tablePrefix = "aj";
        String moduleName = "aj";
        String[] tables = {
                "jz_manage_history"
        };

//        String moduleName = "manager";
//        String tablePrefix = "jx_";
//        String[] tables = {"jx_admin_user"};

        AutoGenerator generator = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("jexx");
        gc.setOpen(false);
        gc.setSwagger2(true);
        gc.setIdType(IdType.ASSIGN_UUID);
        generator.setGlobalConfig(gc);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driver);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        generator.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent(packetName);
        generator.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return StringUtil.format("{}/src/main/java/{}/{}/mapper/xml/{}Mapper.xml", projectPath, packetName.replace('.', '/'), moduleName, tableInfo.getEntityName());
            }
        });
        cfg.setFileOutConfigList(focList);
        generator.setCfg(cfg);


        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        templateConfig.setXml(null);
        generator.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass();
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
//        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(tables);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(tablePrefix);

        List<TableFill> fillList = new ArrayList<>();
        fillList.add(new TableFill("createTime", FieldFill.INSERT));
        fillList.add(new TableFill("updateTime", FieldFill.INSERT_UPDATE));
        fillList.add(new TableFill("createUserId", FieldFill.INSERT));
        fillList.add(new TableFill("updateUserId", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(fillList);

        generator.setStrategy(strategy);
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }

}
